package com.example.datn.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.Inflater;

import com.example.datn.DeveloperKey;
import com.example.datn.R;
import com.example.datn.objects.VideoObject;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubeThumbnailLoader;
import com.google.android.youtube.player.YouTubeThumbnailLoader.ErrorReason;
import com.google.android.youtube.player.YouTubeThumbnailView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class VideoItemAdapter extends ArrayAdapter<VideoObject>{

	Activity context = null;
	ArrayList<VideoObject> myArray = null;
	private final List<View> entryViews;
	int layoutId;
	private final Map<YouTubeThumbnailView, YouTubeThumbnailLoader> thumbnailViewToLoaderMap;
	private final ThumbnailListener thumbnailListener;
	private static LayoutInflater inflater = null;
	
	//private boolean labelsVisible;
	
	public VideoItemAdapter(Activity _context, int _layoutid, ArrayList<VideoObject> arr) {
		super(_context, _layoutid, arr);
		this.context = _context;
		this.myArray = arr;
		this.layoutId = _layoutid;
		
		entryViews = new ArrayList<View>();
		thumbnailViewToLoaderMap = new HashMap<YouTubeThumbnailView, YouTubeThumbnailLoader>();		
		thumbnailListener = new ThumbnailListener();
		
		//labelsVisible = true;
	}

	public void releaseLoaders() {
	     for (YouTubeThumbnailLoader loader : thumbnailViewToLoaderMap.values()) {
	        loader.release();
	      }
	}

	@Override
	public int getCount() {		
		return myArray.size();
	}

	@Override
	public VideoObject getItem(int position) {
		return myArray.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		VideoObject video = myArray.get(position);
		ViewHolder holder;
		
		if (view == null) {
	        // 1) The view has not yet been created - we need to initialize the YouTubeThumbnailView.
			inflater = context.getLayoutInflater();
	        view = inflater.inflate(R.layout.my_video_item, parent, false);
	        
	        holder = new ViewHolder();
	        holder.img_video = (YouTubeThumbnailView) view.findViewById(R.id.img_thumbnail);
	        holder.tv_video_name = ((TextView) view.findViewById(R.id.tv_video_name));
	        holder.img_video.setTag(video.videoId);
	        holder.img_video.initialize(DeveloperKey.DEVELOPER_KEY, thumbnailListener);
	        
	        view.setTag(holder);
	      } else {
	    	holder = (ViewHolder)view.getTag();   
	    	  
	    	holder.img_video = (YouTubeThumbnailView) view.findViewById(R.id.img_thumbnail);
	        YouTubeThumbnailLoader loader = thumbnailViewToLoaderMap.get(holder.img_video);
	        if (loader == null) {
	          // 2) The view is already created, and is currently being initialized. We store the
	          //    current videoId in the tag.
	          holder.img_video.setTag(video.videoId);
	        } else {
	          // 3) The view is already created and already initialized. Simply set the right videoId
	          //    on the loader.
	          holder.img_video.setImageResource(R.drawable.loading_thumbnail);
	          loader.setVideo(video.videoId);
	        }
	      }	
		
		holder.tv_video_name.setText(video.videoName);				
		return view;
	}
	
	
	private static class ViewHolder {
	    public YouTubeThumbnailView img_video;
	    public TextView tv_video_name;
	}

	private final class ThumbnailListener implements YouTubeThumbnailView.OnInitializedListener,
				YouTubeThumbnailLoader.OnThumbnailLoadedListener {

		@Override
		public void onInitializationSuccess(YouTubeThumbnailView view, YouTubeThumbnailLoader loader) {
			loader.setOnThumbnailLoadedListener(this);
			thumbnailViewToLoaderMap.put(view, loader);
			view.setImageResource(R.drawable.loading_thumbnail);
			String videoId = (String) view.getTag();
			loader.setVideo(videoId);
			//Toast.makeText(getContext(), "1", Toast.LENGTH_LONG).show();
		}

		@Override
		public void onInitializationFailure(
			YouTubeThumbnailView view, YouTubeInitializationResult loader) {
			//view.setImageResource(R.drawable.no_thumbnail);			
			//Toast.makeText(getContext(), "2:" + loader.toString(), Toast.LENGTH_LONG).show();
		}

		@Override
		public void onThumbnailLoaded(YouTubeThumbnailView view, String videoId) {
			//Toast.makeText(getContext(), "3", Toast.LENGTH_LONG).show();
		}

		@Override
		public void onThumbnailError(YouTubeThumbnailView view, ErrorReason errorReason) {
			//Toast.makeText(getContext(), "4", Toast.LENGTH_LONG).show();
			//view.setImageResource(R.drawable.no_thumbnail);
		}
	}
}
